$(function () {
  // ****** 时间显示 *********************
  function getWeekDate(time = new Date()) {
    let weeks = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
    let date = new Date(time);
    let day = date.getDay();
    return weeks[day];
  }
  var timeInterval = setInterval(function () {
    var currentDate = new Date();
    var timezone = 8; //东8时区
    var offset_GMT = currentDate.getTimezoneOffset();
    var today = new Date(currentDate.getTime() + offset_GMT * 60 * 1000 + timezone * 60 * 60 * 1000);

    var year = today.getFullYear();
    var month = ('0' + (today.getMonth() + 1)).slice(-2);
    var date = ('0' + today.getDate()).slice(-2);
    var hours = ('0' + today.getHours()).slice(-2);
    var minutes = ('0' + today.getMinutes()).slice(-2);
    var seconds = ('0' + today.getSeconds()).slice(-2);
    var datetime = year + "-" + month + "-" + date + " " + hours + ":" + minutes + ":" + seconds;
    document.getElementById("dateweek").innerText = getWeekDate();
    document.getElementById("nowdatetime").innerText = datetime;
  }, 1000);

  // ******* 获取天气数据 ****************************************
  function getWeather() {
    $.get('https://api.seniverse.com/v3/weather/daily.json?key=SV9ceJddfbsxu4rJC&location=xuzhou&language=zh-Hans&unit=c&start=0&days=2', function (weather) {
      const weatherData = weather.results[0].daily || [];
      const text = weatherData[0].text_day !== weatherData[0].text_night ? weatherData[0].text_day + "转" + weatherData[0].text_night : weatherData[0].text_day;
      const temp = weatherData[0].low + "~" + weatherData[0].high + '℃';
      $("#weather").html(text + '&nbsp;' + temp);
    });
  }
  var weatherInterval = setInterval(getWeather, 1000 * 60 * 60 * 24); // 每天更新一次天气数据

  // *************** 对话框开启/关闭事件处理
  $(".close").click(function () {
    $('.min').hide();
    $('.max').show();
    $('#zoomImage').css({ transform: 'scale(1)' });
    $('#myDialog').removeClass('dialog-full').hide(600);
    $('#dialog-layer').hide();
  });
  $(".max").click(function () {
    $(this).hide();
    $('.min').show();
    $('#myDialog').addClass('dialog-full');
  });
  $(".min").click(function () {
    $(this).hide();
    $('.max').show();
    $('#myDialog').removeClass('dialog-full');
  });
  $(document).keydown(function (e) {
    if (e.keyCode === 27 && $('.max').is(':visible')) {
      $('#zoomImage').css({ transform: 'scale(1)' });
      $('#myDialog').hide(600);
      $('#dialog-layer').hide();
    } else {
      $('.min').hide();
      $('.max').show();
      $('#myDialog').removeClass('dialog-full');
    }
  });

  // ********* 页面初始化调用
  getWeather();

  // ********* 在页面即将关闭时清理 interval
  window.onbeforeunload = function() {
    clearInterval(timeInterval);
    clearInterval(weatherInterval);
    return null;
  };
});